/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pdf;

import com.itextpdf.text.Element;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.OutputStream;

/**
 *
 * @author chapmand
 */
public class GenericPDFCreator {
    private String leftOver = null;

    private final int borderSize = 50;
    private static final Font TITLE_FONT = FontFactory.getFont(FontFactory.HELVETICA_BOLD);
    
    public void createTablePDF(OutputStream outputStream, final String headerCSV, final String tableCSV, String title){
        try{
            Document document = new Document(PageSize.A4, borderSize, borderSize, borderSize, borderSize);
            //PdfWriter.getInstance(document, outputStream);   
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            Paragraph ttl = new Paragraph(title, TITLE_FONT);
            ttl.setAlignment(Element.ALIGN_CENTER);
            document.add(ttl);
            
            //Display Blank Space.
            document.add( Chunk.NEWLINE );
            
            String[] headers = headerCSV.split(",");
        
            /* Step-2: Initialize PDF documents - logical objects */
            PdfPTable pdfTbl = new PdfPTable(headers.length);
            PdfPCell table_cell;
            
            //Set headers
            for(String str : headers){
                table_cell = new PdfPCell(new Phrase(str, TITLE_FONT));
                pdfTbl.addCell(table_cell);
            }

            String[] csvLineArray = tableCSV.split("\n");
            leftOver = null;
            int count = 0;
            for (final String line : csvLineArray) {
                count++;
                if (count >= 40) {
                    if (leftOver == null) {
                        leftOver = line + "\n";
                    } else {
                        leftOver += line + "\n";
                    }
                } else {
                    String[] csvElementArray = line.split(",");
                    for(String csvElement : csvElementArray){
                        table_cell = new PdfPCell(new Phrase(csvElement));
                        pdfTbl.addCell(table_cell);
                    }
                }
            }
            /* Step -4: Attach table to PDF and close the document */
            document.add(pdfTbl);
            
            document.close();
        }catch(DocumentException ex){
            
        }
    }   
    
    public void createStringPDF(OutputStream outputStream, final String str, String title){
        try{
            Document document = new Document(PageSize.A4, borderSize, borderSize, borderSize, borderSize);
            //PdfWriter.getInstance(document, outputStream);   
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            Paragraph ttl = new Paragraph(title, TITLE_FONT);
            ttl.setAlignment(Element.ALIGN_CENTER);
            document.add(ttl);
            
            //Display Blank Space.
            document.add( Chunk.NEWLINE );
            
            document.add(new Paragraph(str));
            
            document.close();
        }catch(DocumentException ex){
            
        }
    }    
    
}
